<?php

/**
 * BannerTypeForm class.
 * BannerTypeForm is the data structure for keeping
 * web Ad form data. It is used by the 'banner' action of 'BannerController'.
 */
class BannerTypeForm extends Banner
{
	/**
	 * Declares the validation rules.
	 * The rules state that username and password are required,
	 * and password needs to be authenticated.
	 */
	public function rules()
	{
		return array(
			// website_title and website_url are required
            array('name','unique'),
			array('name', 'match', 'pattern' => '/^[a-zA-Z][0-9_]*/', 'message' => Yii::t('backend', 'Must begin with alphabet and can only includes alphabet,_,and number')),
			array('name, tips, input_type, type', 'required'),
            array('sort, autoload', 'numerical', 'integerOnly'=>true, 'min'=>0),
			array('value','safe'),
			//array('ad', 'file', 'types' => array('png', 'jpg', 'jpeg', 'gif', 'webp')),
			// 验证字符串 string
			//array('website_title, website_email, website_language, website_icp, website_statics_script, website_timezone, website_url, seo_title, seo_keywords, seo_description', 'boolean'),
			//array('smtp_port', 'numerical', 'integerOnly'=>true, 'min'=>0, 'max'=>65535),
		);
	}

	/**
	 * Declares attribute labels.
	 */
	public function attributeLabels()
	{
		return array(
            'name' => Yii::t('app', '名称'),
            'input_type' => Yii::t('app', 'Ad Type'),
            'tips' => Yii::t('app', '描述'),
            'ad' => Yii::t('app', '广告类容'),
            'link' => Yii::t('app', 'Jump Link'),
            'desc' => Yii::t('app', 'Ad Explain'),
            'autoload' => Yii::t('app', 'Status'),
            'sort' => Yii::t('app', 'Sort'),
            'target' => Yii::t('app', 'Target'),
            'created_at' => Yii::t('app', 'Created At'),
            'updated_at' => Yii::t('app', 'Updated At'),
		);
	}

    public function afterFind()
    {
        if( empty( $this->value ) ) $this->value = "[]";
        $banners = json_decode($this->value, true);
        $models = [];
        foreach ($banners as $banner){
            $temp = [
                'sign' => $banner['sign'],
                'img' => $banner['img'],
                'target' => $banner['target'],
                'desc' => $banner['desc'],
                'link' => $banner['link'],
                'sort' => $banner['sort'],
                'status' => $banner['status'],
            ];
            $models[$banner['sign']] = new BannerForm($temp);
        }
        $this->value = $models;
        parent::afterFind();
    }

    public function beforeSave($insert)
    {
        $banners = !is_array( $this->value ) ? [] : $this->value;
        $array = [];
        foreach ($banners as $banner){
            $temp = [
                'sign' => $banner['sign'],
                'img' => $banner['img'],
                'target' => $banner['target'],
                'desc' => $banner['desc'],
                'link' => $banner['link'],
                'sort' => $banner['sort'],
                'status' => $banner['status'],
            ];
            $array[] = $temp;
        }
        $this->value = json_encode( $array );
        $this->type = self::TYPE_BANNER;
        return parent::beforeSave($insert);
    }

    public function beforeDelete()
    {
        if( !empty($this->value) ) {
            $this->addError("id", Yii::t('app', 'Delete failed, banner existed'));
            return false;
        }
        return parent::beforeDelete();
    }


}
